<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterAdmin.master"
    CodeFile="Events.aspx.cs" validateRequest="false" Inherits="Events"  %>

<%@ Register TagPrefix="FTB" Namespace="FreeTextBoxControls" Assembly="FreeTextBox" %>

<asp:Content ID="Content1" Runat="Server" ContentPlaceHolderID="PageContent" >
    <script src="http://maps.google.com/maps?file=api&v=2.x&key=<%=GetGmapKey()%>" type="text/javascript"></script>
	<script type="text/javascript">
			var map;
			var marker;
			var markerLat;
			var markerLong;
			var textboxLat = "ctl00_PageContent_FormView1_ev_latTextBox"
			var textboxLong = "ctl00_PageContent_FormView1_ev_longTextBox"
			
			//map data
			function start() {
				markerLat = $(textboxLat).value;
				markerLong = $("ctl00_PageContent_FormView1_ev_longTextBox").value;
				load();
			}
			
			function load() {	
		   		//load map
		      if (GBrowserIsCompatible()) {
			  
					var mapCenter = new GLatLng(markerLat, markerLong);
					map = new GMap2($("map"));
					map.addControl(new GSmallMapControl());
					map.setCenter(mapCenter, 14);
					geocoder = new GClientGeocoder();
					
					var point = new GLatLng(markerLat, markerLong);
					marker = new GMarker(point, {draggable: true});
					GEvent.addListener(marker, "dragend", function() {
						var newPos = marker.getPoint();
						$(textboxLat).value = newPos.lat();
						$(textboxLong).value = newPos.lng();
					});
					map.addOverlay(marker);
				
		    	}
		  	}
			
			function showAddress(address) {
				geocoder.getLatLng(
					address,
					function(point) {
						if (!point) {
							alert(address + " not found");
						} else {
							map.setCenter(point, 15);
							marker.setPoint(point);
							$(textboxLat).value = point.lat();
							$(textboxLong).value = point.lng();
							marker.show();
						}
					}
				);
				geocoder.reset();
			}
			
			window.onload = function() {
			    start();
			}
			
			/*window.onunload = function() {
			    GUnload();
			}*/
    </script>
 
    <div class="divBlock">
    
        <asp:GridView ID="grdEvents" runat="server" AutoGenerateColumns="False" CellPadding="4"
            ForeColor="#333333" GridLines="None" OnSorting="SortGrdEvents" AllowSorting="True" 
            OnSelectedIndexChanging="grdEventsCommand" OnRowDeleting="grdEventsDelete" PageSize="25" AllowPaging="True" 
            OnPageIndexChanging="grdPageIndexChanging" OnRowDataBound="grdEventsDataBound">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:ButtonField DataTextField="ev_id" CommandName="Select" HeaderText="ID" SortExpression="ev_id"  />
                <asp:BoundField DataField="et_name" HeaderText="Event" SortExpression="et_name" />
                <asp:BoundField DataField="ev_date" HeaderText="Date" SortExpression="ev_date" DataFormatString="{0:dd/MM/yyyy}" />
                <asp:BoundField DataField="ev_title" HeaderText="Title" SortExpression="ev_title" />
                <asp:BoundField Visible="false" DataField="ev_status" SortExpression="ev_status" />
                <asp:ButtonField CommandName="Delete" Text="Delete" />
            </Columns>
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
     </div>
     <div class="divBlock">
        <asp:FormView DefaultMode="Insert" ID="FormView1" runat="server" CellPadding="4" DataKeyNames="ev_id" 
            DataSourceID="SqlDataSource1" ForeColor="#333333" OnItemUpdated="FormView1_upd" OnItemInserted="FormView1_ins" >
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Justify" />
            <EditRowStyle BackColor="#999999" />
            <EditItemTemplate>
                ID:
                <asp:Label ID="ev_idLabel1" runat="server" Text='<%# Eval("ev_id") %>'></asp:Label>&nbsp;
                Event type:
                <asp:DropDownList ID="ev_typeDropDownList" runat="server" 
                        DataSourceID="SqlDataSource2"
                        DataTextField="et_name" 
                        DataValueField="et_id" 
                        SelectedValue='<%# Bind("ev_type") %>' />&nbsp;
                Date:
                <asp:TextBox ID="ev_dateTextBox" runat="server" Columns="10" Text='<%# Bind("ev_date", "{0:dd MMM yyyy}") %>'>
                </asp:TextBox><br />
                Long/Lat:
                <asp:TextBox Columns="10" ID="ev_longTextBox" runat="server" Text='<%# Bind("ev_long") %>'>
                </asp:TextBox>&nbsp;
                <asp:TextBox Columns="10" ID="ev_latTextBox" runat="server" Text='<%# Bind("ev_lat") %>'>
                </asp:TextBox><br />
                Title:
                <asp:TextBox Columns="25" ID="ev_titleTextBox" runat="server" Text='<%# Bind("ev_title") %>'>
                </asp:TextBox><br />
                Source:
                <asp:TextBox ID="ev_sourceTextBox" runat="server" 
                Columns="25" Text='<%# Bind("ev_source") %>'>
                </asp:TextBox><br />
                <FTB:FreeTextBox DesignModeCss="~/css/cm.css" runat="Server" ID="ev_textTextBox" SupportFolder="~/FreeTextBox/" 
                Text='<%# Bind("ev_text") %>' Width="400px" Height="200px" ButtonSet="OfficeXP"
                ToolbarLayout="Bold,Italic,Underline,Strikethrough;Superscript,Subscript,RemoveFormat;JustifyLeft,JustifyRight,JustifyCenter,JustifyFull;CreateLink,Unlink;"	 />
                <asp:RadioButtonList ID="ev_statusRadioButtonList" runat="server" SelectedValue='<%# Bind("ev_status") %>' RepeatDirection="Horizontal" >
                    <asp:ListItem Value="1">Off</asp:ListItem>
                    <asp:ListItem Value="0">On</asp:ListItem>
                </asp:RadioButtonList>
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update">
                </asp:LinkButton>
                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton><br />
                <input type="text" name="Address" id="Address" size="35" />
                &nbsp;&nbsp;<input type="button" value="Get map" onclick="showAddress($('Address').value); return false;" />
                <div id="map"></div>
            </EditItemTemplate>
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <InsertItemTemplate>
                Event type:
                <asp:DropDownList ID="ev_typeDropDownList" runat="server" 
                        DataSourceID="SqlDataSource2"
                        DataTextField="et_name" 
                        DataValueField="et_id" 
                        SelectedValue='<%# Bind("ev_type") %>' />&nbsp;
                Date:
                <asp:TextBox ID="ev_dateTextBox" runat="server" Columns="10" Text='<%# Bind("ev_date", "{0:dd/MM/yyyy}") %>'>
                </asp:TextBox><br />
                Long/Lat:
                <asp:TextBox Columns="10" ID="ev_longTextBox" runat="server" Text='<%# Bind("ev_long") %>'>
                </asp:TextBox>&nbsp;
                <asp:TextBox Columns="10" ID="ev_latTextBox" runat="server" Text='<%# Bind("ev_lat") %>'>
                </asp:TextBox><br />
                Title:
                <asp:TextBox Columns="25" ID="ev_titleTextBox" runat="server" Text='<%# Bind("ev_title") %>'>
                </asp:TextBox><br />
                Source:
                <asp:TextBox ID="ev_sourceTextBox" runat="server" Columns="25" Text='<%# Bind("ev_source") %>'>
                </asp:TextBox><br />
                <FTB:FreeTextBox DesignModeCss="~/css/cm.css" runat="Server" ID="ev_textTextBox" SupportFolder="~/FreeTextBox/" 
                Text='<%# Bind("ev_text") %>' Width="400px" Height="200px" ButtonSet="OfficeXP"
                ToolbarLayout="Bold,Italic,Underline,Strikethrough;Superscript,Subscript,RemoveFormat;JustifyLeft,JustifyRight,JustifyCenter,JustifyFull;CreateLink,Unlink;"	 />
                <asp:RadioButtonList ID="ev_statusRadioButtonList" runat="server" SelectedValue='<%# Bind("ev_status") %>' RepeatDirection="Horizontal" >
                    <asp:ListItem Value="1" Selected="True">Off</asp:ListItem>
                    <asp:ListItem Value="0">On</asp:ListItem>
                </asp:RadioButtonList>
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Insert">
                </asp:LinkButton>
                <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton><br />
                <input type="text" name="Address" id="Address" size="35" />
                &nbsp;&nbsp;<input type="button" value="Get map" onclick="showAddress($('Address').value); return false;" />
                <div id="map"></div>
            </InsertItemTemplate>
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        </asp:FormView>
        
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MainDB %>"
            SelectCommand="SELECT * FROM cm_events ORDER BY ev_id DESC" 
            ConflictDetection="CompareAllValues" 
            OldValuesParameterFormatString="original_{0}" 
            UpdateCommand="UPDATE cm_events SET ev_type = @ev_type, ev_date = @ev_date, ev_long = @ev_long, ev_lat = @ev_lat, 
                ev_title = @ev_title, ev_text = @ev_text, ev_source = @ev_source, ev_status = @ev_status WHERE ev_id = @original_ev_id">
            <UpdateParameters>
                <asp:Parameter Name="ev_type" Type="Int32" />
                <asp:Parameter Name="ev_date" Type="DateTime" />
                <asp:Parameter Name="ev_long" Type="Decimal" />
                <asp:Parameter Name="ev_lat" Type="Decimal" />
                <asp:Parameter Name="ev_title" Type="String" />
                <asp:Parameter Name="ev_text" Type="String" />
                <asp:Parameter Name="ev_source" Type="String" />
                <asp:Parameter Name="ev_status" Type="Int32" />
                <asp:Parameter Name="original_ev_id" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="ev_type" Type="Int32" />
                <asp:Parameter Name="ev_date" Type="DateTime" />
                <asp:Parameter Name="ev_long" Type="Decimal" />
                <asp:Parameter Name="ev_lat" Type="Decimal" />
                <asp:Parameter Name="ev_title" Type="String" />
                <asp:Parameter Name="ev_text" Type="String" />
                <asp:Parameter Name="ev_status" Type="Int32" />
                <asp:Parameter Name="ev_source" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MainDB %>"
            SelectCommand="SELECT * FROM cm_eventtypes ORDER BY et_name">
        </asp:SqlDataSource>
    </div>
    
</asp:Content>